set more off
global user "`c(username)'"
*** Set WD
cd "/Users//$user/Dropbox/SurveyCovid19/"


*** Directories
global data "Descriptive paper/Clean Data/"
global tables "Descriptive paper/Tables/"
global figures "Descriptive paper/Figures/figures inequality/"
global datasave "Descriptive paper/Submission Material Plos One"
global enddate"01may2020"

use "${data}encuesta_covid_pooled_weights.dta", clear

gen year=substr(recordeddate,1,4)
destring year, replace
gen month=substr(recordeddate,6,2)
destring month, replace
gen day=substr(recordeddate,9,2)
destring day, replace
gen date_string=substr(recordeddate,1,10)
g first_answer=mdy(3,27,2020)
g date=mdy(month,day,year)
format date %td
gen dow=dow(date)


rename countryip country
joinby country using "Descriptive paper/Other Data/datos_corona_country.dta", unm(m)

gen days_since_firstcase=date-cases_date1
gen days_since_firstdeath=date-deaths_date1
gen nodeaths=deaths_date1==.
gen under100=cases_date100==.



foreach x in  ingreso_antes ingreso_ahora trabajocasa perdioempleo cerronegocio_dem cerronegocio_gob  {
replace `x'=. if `x'<0
sum `x'
}

gen cerronegocio=max(cerronegocio_dem, cerronegocio_gob)


label var negocios_cerrar "Agrees with closing non-essential biz"
g h_tiempo_cerrar=tiempo_negocios_cerrar>=3
replace h_tiempo_cerrar=. if tiempo_negocios_cerrar==-99 | tiempo_negocios_cerrar==.		//Note: -98 (NA) replied "No" to 'do you believe non-essential business should close', therefore coded as 0
label var h_tiempo_cerrar "Agrees with closing non-essential biz for a month or more"


replace ingreso_antes=2 if ingreso_antes==1
replace ingreso_ahora=2 if ingreso_ahora==1
replace ingreso_antes=9 if ingreso_antes==10
replace ingreso_ahora=9 if ingreso_ahora==10
replace ingreso_antes=ingreso_antes-1
replace ingreso_ahora=ingreso_ahora-1
label define ingres_antes 1 "<1" 2 "1-2" 3 "2-3"  4 "3-4" 5 "4-6" 6 "6-8" 7 "8-11" 8 ">11" 
label values ingreso_antes ingres_antes
label values ingreso_ahora ingres_antes


encode pais, gen(idpais)

*** 
tab days_since_firstcase
tab days_since_firstdeath
replace days_since_firstcase=22 if days_since_firstcase<22
replace days_since_firstcase=51 if days_since_firstcase>50 & days_since_firstcase!=. 

gen weeks_firstcase=.

replace weeks_firstcase=5 if days_since_firstcase>30 & days_since_firstcase<=37
replace weeks_firstcase=6 if days_since_firstcase>37 & days_since_firstcase<=44
replace weeks_firstcase=7 if days_since_firstcase>44 & days_since_firstcase<=51
replace weeks_firstcase=8 if days_since_firstcase>51 & days_since_firstcase<=58


drop if ingreso_antes==.



* Generate IPW for days 30/31 for rest and reestimate
preserve

gen mujer=genero==1 if genero!=3
gen edu_pri=educ<=2
gen edu_sec=educ==3
gen edu_uni=inlist(educ,5,6)

tab ingreso_antes, gen(I)

gen loss=perdioempleo==100 | cerronegocio==100
drop if days_since_firstcase<30

gen treat=inlist(days_since_firstcase,30,31) if days_since_firstcase!=.


global variables nrhogar hayninhos haymayores hambre mujer edu_pri edu_uni edad I1 I2 I3 I7 I8

foreach var in $variables {
	replace `var'=. if `var'<0
}

keep if date>td(01apr2020) & date<td($enddate)

logit treat $variables i.dow
predict prob

gen new_factor=prob if treat==1
replace new_factor=1/(1-prob) if treat==0 

keep _recordid new_factor
tempfile aux
save "`aux'", replace
restore

merge 1:1 _recordid using "`aux'", nogen


keep perdioempleo cerronegocio pop ipw ipw_c new_factor idpais ingreso_antes dow date ingreso_ahora h_tiempo_cerrar  days_since_firstcase 
compress
save "$datasave/data_figures1-3.dta", replace


